<?php

/*
	任务列表（工作人员）
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['types']=array(
	'1'=>'常规模型',
	'2'=>'售后维护',
	'3'=>'服务模型',
);
$return['statuss']=array(
	'0'=>'待处理',
	'1'=>'处理中',
	'2'=>'处理完毕',
	'3'=>'审核完毕',
	'4'=>'审核不通过',
);
$set=pdo_get('jy_workline_set',array('uniacid'=>$_SESSION['uniacid']));
$return['default_type'] = explode("|",$set['default_type']);
$return['service_type'] = explode("|",$set['service_type']);
$return['bug_type'] = explode("|",$set['bug_type']);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'setmid'=>$_SESSION['uid']);
	//类型。1表示开发任务，2表示bug修复任务，3表示客户异常问题处理
	if(!empty($_RQ['groupid'])){
		$params['groupid']=intval($_RQ['groupid']);
		$return['params']['groupid']=$_RQ['groupid'];
	}
	//状态。0表示待处理。1表示处理中。2表示处理完毕待审核。3表示审核通过。
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	//任务名称
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_workline_task',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($list as &$row){
		$row['groupid']=pdo_getcolumn('jy_workline_group',array('id'=>$row['groupid']),'title');
		$row['getmid']=pdo_getcolumn('jy_workline_worker',array('mid'=>$row['getmid']),'name');
		$row['createtime']= date('Y/m/d H:i:s',$row['createtime']);
	}
	$return['groups']=pdo_getall('jy_workline_group',array(),array('id','title'));
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'我发布的'),
			array('op'=>'edit','title'=>'添加任务'),
		),
		'list'=>array('data'=>'list','delall'=>false,'del_one'=>false,'list'=>array(
			array('name'=>'title','title'=>'任务名称','type'=>'string'),
			array('name'=>'credit','title'=>'任务积分','type'=>'string'),
			array('name'=>'groupid','title'=>'任务分类','type'=>'string'),
			array('name'=>'status','title'=>'任务状态','type'=>'radio','options'=>'statuss'),
			array('name'=>'receive_type','title'=>'领取方式','type'=>'radio','options'=>'receive_types'),
			array('name'=>'getmid','title'=>'领取人','type'=>'string'),
			array('name'=>'receive_time','title'=>'领取时间','type'=>'time'),
			array('name'=>'complete_time','title'=>'完成时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'href','op'=>'edit','title'=>'查看任务','show'=>array('status !='=>4)),
		)),
		'search'=>array(
			array('type'=>'select','name'=>'groupid','title'=>'任务分类','option_title'=>'title','options'=>'groups'),
			array('type'=>'radio','name'=>'status','title'=>'任务状态','options'=>'statuss'),
			array('type'=>'string','name'=>'title','title'=>'任务名称'),
		),
	);
	//$return['htmls']=$htmls;
	$return['receive_types']=array(
		'1'=>'管理员指派',
		'2'=>'自己领取',
		'3'=>'发布者指派',
	);
	exi($return);
}

if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!$post['title']){
			exi('请输入标题','error');
		}
		if(!$post['content']){
			exi('请输入内容','error');
		}
		if(!$post['groupid']){
			//exi('请选择组别','error');
		}
		$data=array(
			'title'=>trim($post['title']),
			'uniacid'=>intval($_SESSION['uniacid']),
			'groupid'=>intval($post['groupid']),
			'content'=>trim($post['content']),
			'image'=>ijson_encode($post['image']),
			'web'=>trim($post['web']),
			'ftp'=>trim($post['ftp']),
			'db'=>trim($post['db']),
			'credit'=>intval($post['credit']),
			'default_type'=>intval($post['default_type']),
			'service_type'=>intval($post['service_type']),
			'bug_type'=>intval($post['bug_type']),
			'project_type'=>intval($post['project_type']),
			'jiaji'=>intval($post['jiaji']),
			'setmid'=>$_SESSION['uid'],
			'ordersn'=>get_ordersn('jy_workline_task','ordersn'),
		);
		$id=intval($post['id']);
		if($id){
			$tcpid = pdo_getcolumn('jy_workline_task',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id),'tcpid');
			$tcplog = array('msg'=>trim($post['content']),'image'=>trim($post['image']));
			pdo_update('jy_workline_task',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			pdo_update('jy_workline_tcplog',$tcplog,array('uniacid'=>$_SESSION['uniacid'],'tcpid'=>$tcpid));
		}else{
			$data['createtime']=TIMESTAMP;
			$data['setmid']=$_SESSION['uid'];
			pdo_insert('jy_workline_tcp',array('uniacid'=>$_SESSION['uniacid']));
			$tcpid=pdo_insertid();
			$data['tcpid']=$tcpid;
			pdo_insert('jy_workline_task',$data);
			$tcplog = array('uniacid'=>$_SESSION['uniacid'],'setmid'=>$_SESSION['uid'],'tcpid'=>$tcpid,'msg'=>trim($post['content']),'image'=>trim($post['image']),'createtime'=>TIMESTAMP);
			pdo_insert('jy_workline_tcplog',$tcplog);
		}
		exi('操作成功！','','list');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_workline_task',array('id'=>$id));
		$item['image']=$item['image']?json_decode($item['image'],true):array();
	}else{
		$item=array();
	}
	
	$return['groups']=pdo_getall('jy_workline_group',array(),array('id','title'));
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回任务列表'),
		),
		'desc'=>'设置任务信息',
		'edit'=>array(
			array('type'=>'string','name'=>'title','title'=>'任务名称'),
			array('type'=>'switch','name'=>'jiaji','title'=>'是否加急'),
			array('type'=>'select','name'=>'groupid','title'=>'任务分组','option_title'=>'title','options'=>'groups'),
			array('type'=>'radio','name'=>'type','title'=>'任务模型','options'=>'types'),
			array('type'=>'radio','name'=>'default_type','title'=>'小分类','options'=>'default_type','show'=>array('type'=>1)),
			array('type'=>'radio','name'=>'bug_type','title'=>'小分类','options'=>'bug_type','show'=>array('type'=>2)),
			array('type'=>'radio','name'=>'service_type','title'=>'小分类','options'=>'service_type','show'=>array('type'=>3)),
			array('type'=>'text','name'=>'content','title'=>'任务内容'),
			array('type'=>'images','name'=>'image','title'=>'任务截图'),
			array('type'=>'text','name'=>'web','title'=>'网站资料','show'=>array('type'=>3)),
			array('type'=>'text','name'=>'ftp','title'=>'ftp','show'=>array('type'=>3)),
			array('type'=>'text','name'=>'db','title'=>'数据库资料','show'=>array('type'=>3)),
			array('type'=>'string','name'=>'credit','title'=>'任务积分'),
		),
	);
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_workline_task',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	}
	exi('删除成功！');
}